package hamaster.jpbc.dao.impl;

import hamaster.jpbc.Emails;
import hamaster.jpbc.User;
import hamaster.jpbc.dao.EmailsDAO;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

public class EmailsDAOHibernateImpl extends CommonDAOHibernateImpl implements EmailsDAO {

	@Override
	public List<Emails> get(User owner) {
		Session session = factory.openSession();
		Criteria criteria = session.createCriteria(Emails.class).
				add(Restrictions.eq("owner", owner));
		@SuppressWarnings("unchecked")
		List<Emails> res = criteria.list();
		return res;
	}

	@Override
	public boolean checkEmailBinding(User user, String email) {
		if (user.getEmail().equals(email))
			return true;
		Session session = factory.openSession();
		Criteria criteria = session.createCriteria(Emails.class).
				add(Restrictions.eq("owner", user)).
				add(Restrictions.eq("email", email));
		List<?> res = criteria.list();
		return res != null && res.size() > 0;
	}
}
